﻿using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Framework.Common.Html;
using Microsoft.VisualBasic;

namespace Framework.Common.Extention
{
    /// <summary>
    /// 字符串处理扩展类
    /// </summary>
    public static class StringExtention
    {
        #region 删除字符串尾部的回车/换行/空格
        /// <summary>
        /// 删除字符串尾部的回车/换行/空格
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RightTrim(this string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return string.Empty;
            }
            return input.TrimEnd(' ', '\r', '\n');
        }
        #endregion

        #region 清除回车和换行
        /// <summary>
        /// 清除给定字符串中的回车及换行符
        /// </summary>
        /// <param name="input">要清除的字符串</param>
        /// <returns>清除后返回的字符串</returns>
        public static string ClearBr(this string input)
        {
            if (string.IsNullOrWhiteSpace(input)) return string.Empty;
            return input.Replace("\r\n", string.Empty);
        }
        #endregion

        #region 字符串截取
        /// <summary>
        /// 从字符串的指定位置截取指定长度的子字符串(不报异常的substring)
        /// </summary>
        /// <param name="input">原字符串</param>
        /// <param name="startIndex">子字符串的起始位置</param>
        /// <param name="length">子字符串的长度</param>
        /// <returns>子字符串</returns>
        public static string CutString(this string input, int startIndex, int length)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return string.Empty;
            }
            if (startIndex >= 0 && length > 0 && startIndex + length <= input.Length)
            {
                return input.Substring(startIndex, length);
            }
            return string.Empty;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string CutString(this string input, int length)
        {
            return input.CutString(0, length);
        }
        /// <summary>
        /// 中英文字符截字
        /// </summary>
        /// <param name="input"></param>
        /// <param name="len"></param>
        /// <param name="tail"> </param>
        /// <returns></returns>
        public static string CutStringCN(this string input, int len, string tail = null)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return string.Empty;
            }

            int l = input.Length;

            for (int i = 0; i < l && i < len; i++)
            {
                if (input[i] > 0xFF)
                    len--;
            }

            if (l < len)
                return input;

            string result = input.Substring(0, len);

            return string.IsNullOrWhiteSpace(tail) ? result : result + tail;
        }
        /// <summary>
        /// 截取html简介
        /// </summary>
        /// <param name="input"></param>
        /// <param name="len"></param>
        /// <param name="tail"></param>
        /// <returns></returns>
        public static string CutHtml(this string input, int len, string tail = null)
        {
            string result = new Html2Summary(input).Truncate(len);
            if (!string.IsNullOrWhiteSpace(tail))
                result = result + tail;
            return result;
        }
        #endregion

        #region 字符串长度
        /// <summary>
        /// 获取中文字符串长度
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int StringLength(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return 0;
            }
            int result = input.Length;
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] > 0xFF)
                    result++;
            }
            return result;
        }

        #endregion

        #region 简繁互换

        /// <summary>
        /// 转换为简体中文
        /// </summary>
        public static string ToSChinese(this string input)
        {
            return Strings.StrConv(input, VbStrConv.SimplifiedChinese);
        }

        /// <summary>
        /// 转换为繁体中文
        /// </summary>
        public static string ToTChinese(this string input)
        {
            return Strings.StrConv(input, VbStrConv.TraditionalChinese);
        }

        #endregion

        #region html 编码解码
        /// <summary>
        /// 返回 HTML 字符串的编码结果
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns>编码结果</returns>
        public static string HtmlEncode(this string input)
        {
            return HttpUtility.HtmlEncode(input);
        }

        /// <summary>
        /// 返回 HTML 字符串的解码结果
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns>解码结果</returns>
        public static string HtmlDecode(this string input)
        {
            return HttpUtility.HtmlDecode(input);
        }
        #endregion

        #region url编码解码

        #region 对url进行编码
        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns>编码结果</returns>
        public static string UrlEncode(this string input)
        {
            return HttpUtility.UrlEncode(input);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="input">字符串</param>
        /// <param name="e"> </param>
        /// <returns>编码结果</returns>
        public static string UrlEncode(this string input, Encoding e)
        {
            return HttpUtility.UrlEncode(input, e);
        }
        #endregion

        #region 对字符串进行url解码
        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns>解码结果</returns>
        public static string UrlDecode(this string input)
        {
            return HttpUtility.UrlDecode(input);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="input">字符串</param>
        /// <param name="e"> </param>
        /// <returns>解码结果</returns>
        public static string UrlDecode(this string input, Encoding e)
        {
            return HttpUtility.UrlDecode(input, e);
        }
        #endregion

        #endregion

        #region 对于 html 进行 encode
        /// <summary>
        /// 对于 html 进行 encode
        /// </summary>
        public static string EncodeHtml(this string strHtml)
        {
            if (!string.IsNullOrWhiteSpace(strHtml))
            {
                strHtml = strHtml.Replace(",", "&def");
                strHtml = strHtml.Replace("'", "&dot");
                strHtml = strHtml.Replace(";", "&dec");
                return strHtml;
            }
            return string.Empty;
        }
        #endregion

        #region 移除所有Html标记
        /// <summary>
        /// 移除所有Html标记
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveHtml(this string content)
        {
            return new Html2Text(content).GetText();
        }
        #endregion

        #region 过滤HTML中的不安全标签
        /// <summary>
        /// 过滤HTML中的不安全标签
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveUnsafeHtml(this string content)
        {
            content = Regex.Replace(content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
            return content;
        }
        #endregion

        //#region 从HTML中获取文本,保留br,p,img
        //static readonly Regex RegEx = new Regex(@"</?(?!br|/?p|img)[^>]*>", RegexOptions.IgnoreCase);
        ///// <summary>
        ///// 从HTML中获取文本,保留br,p,img
        ///// </summary>
        ///// <param name="html"></param>
        ///// <returns></returns>
        //public static string GetTextFromHTML(this string html)
        //{
        //    return RegEx.Replace(html, "");
        //}
        //#endregion

        #region 删除最后一个字符
        /// <summary>
        /// 删除最后一个字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ClearLastChar(this string input)
        {
            return (string.IsNullOrWhiteSpace(input)) ? string.Empty : input.Substring(0, input.Length - 1);
        }
        #endregion

        #region 全角数字转化为数字
        /// <summary>
        /// 将全角数字转换为数字
        /// </summary>
        /// <param name="sbcCase"></param>
        /// <returns></returns>
        public static string SbcCaseToNumberic(this string sbcCase)
        {
            char[] c = sbcCase.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                byte[] b = Encoding.Unicode.GetBytes(c, i, 1);
                if (b.Length == 2)
                {
                    if (b[1] == 255)
                    {
                        b[0] = (byte)(b[0] + 32);
                        b[1] = 0;
                        c[i] = Encoding.Unicode.GetChars(b)[0];
                    }
                }
            }
            return new string(c);
        }
        #endregion

        #region 字符串转化为拼音
        private static readonly int[] PyValue = new[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,  
          -20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,  
          -19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,  
          -19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,  
          -18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526,  
          -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,  
          -18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,  
          -17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,  
          -17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,  
          -16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,  
          -16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,  
          -15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,  
          -15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,  
          -15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,  
          -14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,  
          -14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,  
          -14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,  
          -14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,  
          -13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,  
          -13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,  
          -12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,  
          -12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,  
          -11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,  
          -11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,  
          -10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254};
        private static readonly string[] PyStr = new[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",  
        "bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",  
        "cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",  
        "cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",  
        "dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",  
        "ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",  
        "hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",  
        "jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",  
        "keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",  
        "leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",  
        "ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",  
        "na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",  
        "nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",  
        "pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",  
        "ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",  
        "sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",  
        "shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",  
        "tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",  
        "wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",  
        "xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",  
        "yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",  
        "zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",  
        "zi","zong","zou","zu","zuan","zui","zun","zuo"};

        public static string ToSpell(this string chrstr)
        {
            string returnstr = string.Empty;
            char[] nowchar = chrstr.ToCharArray();
            for (int j = 0; j < nowchar.Length; j++)
            {
                byte[] array = Encoding.Default.GetBytes(nowchar[j].ToString(CultureInfo.InvariantCulture));
                int i1 = array[0];
                int i2 = array[1];
                int chrasc = i1 * 256 + i2 - 65536;
                if (chrasc > 0 && chrasc < 160)
                {
                    returnstr += nowchar[j];
                }
                else
                {
                    for (int i = (PyValue.Length - 1); i >= 0; i--)
                    {
                        if (PyValue[i] <= chrasc)
                        {
                            returnstr += PyStr[i];
                            break;
                        }
                    }
                }
            }
            return returnstr;
        }
        #endregion
    }
}
